package com.chenyanqing.model.two.utils;


import com.chenyanqing.model.two.factory.TransactionAnnotationProxyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 负责手动事务的管理（开启、提交、回滚）
 */
public class TransactionalManager {

    private static Logger logger = LoggerFactory.getLogger(TransactionalManager.class);


    private TransactionalManager(){

    }
    private static TransactionalManager transactionalManager = new TransactionalManager();

    public static TransactionalManager getInstance(){
        return transactionalManager;
    }

    /**
     * 开启事务
     * @throws SQLException
     */
    public void beginTransaction() throws SQLException {
        Connection connection = JDBCConnectionUtils.getJDBCConnectionUtils().getCurrentThreadConnection();
        connection.setAutoCommit(false);////开启事务
        logger.info("beginTransaction");

    }


    /**
     * 提交事务
     * @throws SQLException
     */
    public void  commit() throws SQLException {
        Connection connection = JDBCConnectionUtils.getJDBCConnectionUtils().getCurrentThreadConnection();
        connection.commit(); ////提交事务
        logger.info("commit");

    }


    /**
     * 回滚事务
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        Connection connection = JDBCConnectionUtils.getJDBCConnectionUtils().getCurrentThreadConnection();
        connection.rollback();//回滚事务
        logger.info("rollback");

    }



}
